package com.dbsop.operation.query.transport;

import com.dbsop.domain.EsCondition;
import com.dbsop.domain.transport.EsTransportCondition;
import com.dbsop.domain.EsOrder;
import com.dbsop.domain.transport.rank.TransportRanking;
import com.dbsop.common.enums.Order;
import com.dbsop.operation.query.QuerySearch;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.util.List;
import java.util.Set;

@Slf4j
public class TcQuerySearch extends QuerySearch {

    private TcBoolQueries boolQueries;

    private String[] fields = null;
    private SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();


    public TcQuerySearch(EsCondition esCondition) {
        super(esCondition);
        boolQueries = new TcBoolQueries((EsTransportCondition) esCondition);
        initFields((EsTransportCondition) esCondition);
        initSourceBUilder();
        initRank();
    }

    private void initFields(EsTransportCondition esTransportCondition) {
        Set<String> fields = esTransportCondition.getFields();
        if (fields == null || fields.size() == 0)return;
        this.fields = new String[fields.size()];
        int i = 0 ;
        for (String field : fields) {
            this.fields[i] = field;
            i++;
        }
    }

    public void initRank(){
        TransportRanking ranking = (TransportRanking) this.esCondition.getRanking();
        if (ranking == null){
            return;
        }
        CollapseBuilder builder = ranking.getBuilder();
        if (builder == null){
            return;
        }
        sourceBuilder.collapse(builder);
    }

    public void initSourceBUilder(){
        sourceBuilder.query(boolQueries.getBoolQueryBuilder());

        sourceBuilder.from(esCondition.getFrom());
        sourceBuilder.size(esCondition.getSize());
        order();
        if (this.fields!=null)sourceBuilder.fetchSource(fields,null);


    }

    public SearchRequest getSearchRequest(){
        SearchRequest searchRequest = new SearchRequest(esCondition.getIndices());
        searchRequest.source(sourceBuilder);
        log.info("查询操作:" + sourceBuilder.toString());
        return searchRequest;
    }


    public SearchRequest getSearchRequestAgg() {
        SearchRequest searchRequest = new SearchRequest(esCondition.getIndices());
        searchRequest.source(sourceBuilder);
        return searchRequest;
    }

    public void setSearchRequest(SearchRequest searchRequest){
        searchRequest.indices(esCondition.getIndices());
        searchRequest.source(sourceBuilder);
    }

    public String[] getFields() {
        return fields;
    }

    /**
     * 排序
     */
    public void order(){
        List<EsOrder> esOrders = esCondition.getEsOrders();
        if (esOrders == null || esOrders.size() == 0){
            return;
        }
        for (EsOrder esOrder : esOrders) {
            List<String> fields = esOrder.getFields();
            Order order = esOrder.getOrder();
            for (String field : fields) {
                FieldSortBuilder fieldSortBuilder = new FieldSortBuilder(field);
                switch (order){
                    case ASC:
                        fieldSortBuilder.order(SortOrder.ASC);
                        break;
                    case DESC:
                        fieldSortBuilder.order(SortOrder.DESC);
                        break;
                }
                sourceBuilder.sort(fieldSortBuilder);
            }
        }
    }


}
